package ltd.jdsoft.cute.bank.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ltd.jdsoft.cute.bank.dao.BankBinDAO;
import ltd.jdsoft.cute.bank.model.BankBin;

@Service
public class BankBinService {

  @Autowired
  private BankBinDAO bankBinDAO;

  /**
   * 根据银行卡号获取对应的银行编码
   * 
   * @param bankNo 银行卡号
   * @return
   */
  public BankBin getByCardNo(String cardNo) {
    // 根据统计，卡BIN长度是6位的大概在30%，9位的大概在40%，因此先尝试固定位数的获取
    BankBin bankBin = bankBinDAO.getByBin(cardNo.substring(0, 6));
    if (bankBin == null) {
      bankBin = bankBinDAO.getByBin(cardNo.substring(0, 9));
    }

    // 如果不是6位和9位，那么执行sql查询，速度稍微慢一点，但是数据量不大(1w)以内，还是可以接受的
    if (bankBin == null) {
      bankBin = bankBinDAO.getByCardNo(cardNo);
    }

    return bankBin;
  }
}
